package com.java.demo.demo;

import com.alibaba.fastjson.JSON;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class GroupDemo {

    public static void main(String[] args) {
        User user1 = new User("1", "测试", "111");
        User user2 = new User("1", "Demo", "111");
        User user3 = new User("2", "测试", "222");
        User user4 = new User("3", "测试", "333");
        User user5 = new User("4", "Demo", "444");
        User user6 = new User("2", "Demo", "222");
        User user7 = new User("5", "测试", "555");
        User user8 = new User("5", "测试", "555");
        User user9 = new User("5", "测试", "555");

        List<User> userList = new ArrayList<>(2);
        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
        userList.add(user4);
        userList.add(user5);
        userList.add(user6);
        userList.add(user7);
        userList.add(user8);
        userList.add(user9);

        Map<String, List<User>> map = userList.stream().collect(Collectors.groupingBy(User::getUserId));
        System.out.println(JSON.toJSONString(map));
    }

    @Data
    public static class User {

        private String userId;

        private String type;

        private String userName;

        public User(String userId, String type, String userName) {
            this.userId = userId;
            this.type = type;
            this.userName = userName;
        }
    }
}
